package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Map<String, Object> currentCatalog(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return getCatalogtMap(category);
    }



    @Override
    public Map<String, Object> catalogIndex() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        categoryExample.setOrderByClause("id asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        Category currentCategory = categoryList.get(0);
        Map<String, Object> map = getCatalogtMap(currentCategory);
        map.put("categoryList",categoryList);
        return map;
    }

    private Map<String, Object> getCatalogtMap(Category category) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(category.getId());
        criteria.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        Map<String, Object> map = new HashMap<>();
        map.put("currentCategory",category);
        map.put("currentSubCategory",categoryList);
        return map;
    }

}
